package com.wcity.manage.web.property;

import com.wcity.manage.common.context.SessionKey;
import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.model.Block;
import com.wcity.manage.dao.model.Property;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.property.PropertyFeeService;
import com.wcity.manage.service.property.PropertyOnDutyService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 小区物业费组成管理模块
 * Created by zm on 2016/12/12.
 */
@Controller
public class PropertyFeeAct {

    @Resource private PropertyFeeService propertyFeeService;
    @Resource private PropertyOnDutyService propertyOnDutyService;

    /**
     * 分页查询表单
     * @param propertyFee
     * @param pageModel
     * @param request
     * @return
     * @Author zm
     */
    @RequestMapping("pro_Fee_list")
    public String proFeeList (Property propertyFee , PageModel pageModel, HttpServletRequest request ){
        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        propertyFee.setUserId(user.getId());
        pageModel.setParams( propertyFee );
        request.setAttribute( "pageModel", pageModel );
        request.setAttribute( "propertyFee", propertyFee );
        request.setAttribute( "userId", user.getId() );
        request.setAttribute( "workNo", user.getWorkNo() );
        request.setAttribute( "list", propertyFeeService.getPayTypeListPage( pageModel ) );
        return "propertyFee/propertyFeeList";
    }

    /**
     * 跳转到添加页面
     * @param request
     * @return
     */
    @RequestMapping("to_Fee_add")
    public String toFeeAdd (HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        request.setAttribute( "blockList", propertyFeeService.getblockName( user.getId()) );
        request.setAttribute( "payTypeList",propertyFeeService.getPayType( user.getId()) );
        return "propertyFee/propertyFeeAdd";
    }

    /**
     * 添加
     * @param propertyFee
     * @param request
     * @param response
     */
    @RequestMapping("fee_add")
    public void feeAdd (Property propertyFee , HttpServletRequest request, HttpServletResponse response ) {

        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        int count = propertyFeeService.selectBlockNameCount( propertyFee.getBlockId()+"" );
        if(count > 0){
            RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "已经添加过，无需再添加", "pro_Fee_list", "", "closeCurrent", "" ) );
            return;
        }
        Block block = propertyOnDutyService.getUserInfo ( propertyFee.getBlockId()+"" );

        propertyFee.setProBlockName( block.getBlockName() );
        propertyFee.setUserId(user.getId());
        propertyFee.setCreateTime(new Date());

        String code = "200";
        String meg = "添加成功";
        try{
            int success = propertyFeeService.feeAdd(propertyFee);
            if( success == 0 ){
                code = "300";
                meg = "添加失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "添加失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "pro_Fee_list", "", "closeCurrent", ""));
    }

    /**
     * 跳转到登入页面修改
     * @param request
     */
    @RequestMapping("to_fee_edit")
    public String  toFeeEdit (HttpServletRequest request) {
        String id = request.getParameter("id");
        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        request.setAttribute( "blockList", propertyFeeService.getblockName( user.getId()) );
        request.setAttribute( "payTypeList",propertyFeeService.getPayType( user.getId()) );
        request.setAttribute( "list",propertyFeeService.getInfo(id));
        return "propertyFee/propertyFeeEdit";
    }

    /**
     * 修改
     * @param propertyFee
     * @param response
     */
    @RequestMapping("fee_edit")
    public void feeEdit (Property propertyFee, HttpServletResponse response) {

        String code = "200";
        String meg = "修改成功";
        try{
            int success = propertyFeeService.updatePropertyFee(propertyFee);
            if( success == 0 ){
                code = "300";
                meg = "修改失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "修改失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "pro_Fee_list", "", "closeCurrent", ""));
    }

    /**
     * 删除
     * @param request
     * @param response
     */
    @RequestMapping("fee_delect")
    public void feeDelect ( HttpServletRequest request,HttpServletResponse response ) {
        String id = request.getParameter("id");

        String code = "200";
        String meg = "删除成功";
        try{
            int success = propertyFeeService.feeDelect(id);
            if( success == 0 ){
                code = "300";
                meg = "删除失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "删除失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "pro_Fee_list", "", "", ""));
    }
}
